<?php
//Name: util.php
//Purpose: This script is a place for misc functions used throughout the application.
//CreateDate: 10/01/12

function loginUser($usernameToLogin, $passwordToLogin)
{
	$returnValue = false;
	//to seperate each line in to an array entry
	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

	//to seperate each user in to an array
	foreach ($usersArray as &$user)
	{
		$user = explode("|", $user);
	}

	//to match the username and password
	foreach($usersArray as $userExplode)
	{
		if($userExplode[0] == $usernameToLogin && $userExplode[1] == $passwordToLogin)
		{
			
			return $userExplode[0];
// 			//successful login
// 			session_name('login');
// 			session_start();
// 			$_SESSION['username'] = $userExplode[0];
// 			if(end($userExplode) == "*")
// 			{
// 				$_SESSION['isAdmin'] = true;
// 			}
			
// 			session_write_close();
// 			return true;
// 			header("location: /home.php");
		}
	}

	header("location: /home.php");
	return null;
}

function usernameIsAdmin($usernameIsAdmin)
{
	$returnValue = false;
	//to seperate each line in to an array entry
	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

	//to seperate each user in to an array
	foreach ($usersArray as &$user)
	{
		$user = explode("|", $user);
	}

	//to match the username and password
	foreach($usersArray as $userExplode)
	{
		if($userExplode[0] == $usernameIsAdmin)
		{
			//successful login
			session_name('login');
			session_start();
			if(end($userExplode) == "*")
			{
				$_SESSION['isAdmin'] = true;
			}
			session_write_close();
			$returnValue = true;
		}
	}
	return $returnValue;
}

function usernameExists($usernameToCheck)
{
	$returnValue = false;
	//to seperate each line in to an array entry
	$usersArray = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	
	//to seperate each user in to an array
	foreach ($usersArray as &$user)
	{
		$user = explode("|", $user);
	}
	
	//to match the username and password
	foreach($usersArray as $userExplode)
	{
		if($userExplode[0] == $usernameToCheck)
		{
			$returnValue = true;
		}
	}
	
	return $returnValue;
}


?>